#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QRegExpValidator>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->lineEdit_name->setPlaceholderText("请输入姓名");
    ui->lineEdit_name->setClearButtonEnabled(true);
    ui->lineEdit_passwd->setPlaceholderText("请输入密码");
    ui->lineEdit_passwd->setEchoMode(QLineEdit::Password);
    ui->lineEdit_passwd->setClearButtonEnabled(true);

    ui->lineEdit_passwd_2->setPlaceholderText("请再次输入密码");
    ui->lineEdit_passwd_2->setEchoMode(QLineEdit::Password);
    ui->lineEdit_passwd_2->setClearButtonEnabled(true);

    ui->lineEdit_tele->setPlaceholderText("请输入电话");
    ui->lineEdit_name->setClearButtonEnabled(true);

    ui->label_passwd_compare->setText("密码为空");
    QRegExp regexp("^1\\d{10}$");
    ui->lineEdit_tele->setValidator(new QRegExpValidator(regexp));
//    ui->lineEdit_tele->setInputMask("00000000000");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_submit_clicked()
{

    QString sex = ui->radioButton->isChecked()?"男":"女";
    qDebug()<<ui->lineEdit_name->text()
            <<ui->lineEdit_passwd->text()
            <<sex
            <<ui->lineEdit_tele->text();
}

void Widget::on_lineEdit_tele_textEdited(const QString &text)
{
    QString content = text;
    int pos = 0;
    if(ui->lineEdit_tele->validator()->validate(content,pos)== QValidator::Acceptable)
        ui->pushButton_submit->setEnabled(true);
    else
        ui->pushButton_submit->setEnabled(false);
}

void Widget::on_lineEdit_passwd_textEdited(const QString &arg1)
{
    (void)arg1;
    this->compareText();
}

void Widget::on_lineEdit_passwd_2_textEdited(const QString &arg1)
{
    (void)arg1;
    this->compareText();
}

void Widget::compareText()
{
    const QString& pwd1 = ui->lineEdit_passwd->text();
    const QString& pwd2 = ui->lineEdit_passwd_2->text();
    if(pwd1.isEmpty() && pwd2.isEmpty())
        ui->label_passwd_compare->setText("密码为空");
    else if(pwd1 == pwd2)
        ui->label_passwd_compare->setText("密码一致");
    else
        ui->label_passwd_compare->setText("密码不一致");
}

void Widget::changePwdDisplay(QLineEdit* le,bool checked)
{
    if(checked)
        le->setEchoMode(QLineEdit::Normal);
    else
       le->setEchoMode(QLineEdit::Password);
}

void Widget::on_checkBox_toggled(bool checked)
{
    this->changePwdDisplay(ui->lineEdit_passwd,checked);
}

void Widget::on_checkBox_2_toggled(bool checked)
{
    this->changePwdDisplay(ui->lineEdit_passwd_2,checked);
}
